package main

import (
	"bytes"
	"fmt"
	"github.com/gin-gonic/gin"
	"gomicrodemo/changgou-api-gateway/good/client"
	"gomicrodemo/changgou-api-gateway/good/handler"
	gw_client2 "gomicrodemo/changgou-api-gateway/user/client"
	handler2 "gomicrodemo/changgou-api-gateway/user/handler"
	middlewarre2 "gomicrodemo/changgou-api-gateway/user/middlewarre"
	"gomicrodemo/common"
	"io/ioutil"
	"math/rand"
	"time"
)

func main() {
	r := gin.Default()
	api := r.Group("/v1")

	//获取对应的Rand服务的客户端
	userClient := gw_client2.GetUserClient()
	randClient := gw_client2.GetRandClient()
	//获取Rand服务端对应的处理程序的调用对象 : func Getuser_APIHandler(service rand.RandService) *user_APIHandler{}
	user_APIHandler := handler2.GetUserAPIHandler(randClient, userClient)

	//获取随机数
	api.POST("rand", middlewarre2.AuthMiddleware(), user_APIHandler.Rand_POST)
	//用户user服务
	api.POST("registry", user_APIHandler.UserRegistry)
	api.POST("login", loginMiddle(), user_APIHandler.UserLogin)
	//导出和上传
	api.POST("export", middlewarre2.AuthMiddleware(), user_APIHandler.UserExport)
	api.POST("upload", middlewarre2.AuthMiddleware(), user_APIHandler.UploadUserForm)

	//获取对应good服务的客户端
	goodClient := client.GetGoodClient()
	good_APIhandler := handler.GetGoodAPIHandler(goodClient)
	//brands
	api.POST("getBrands", middlewarre2.AuthMiddleware(), good_APIhandler.GetBrands)
	api.POST("updateBrands", middlewarre2.AuthMiddleware(), good_APIhandler.UpdateBrands)
	api.POST("createBrands", middlewarre2.AuthMiddleware(), good_APIhandler.CreateBrands)
	//template
	api.POST("getTemplates", middlewarre2.AuthMiddleware(), good_APIhandler.GetTemplates)
	api.POST("updateTemplate", middlewarre2.AuthMiddleware(), good_APIhandler.UpdateTemplate)
	api.POST("createTemplate", middlewarre2.AuthMiddleware(), good_APIhandler.CreateTemplate)
	//category
	api.POST("getCategorys", middlewarre2.AuthMiddleware(), good_APIhandler.GetCategorys)
	api.POST("updateCategory", middlewarre2.AuthMiddleware(), good_APIhandler.UpdateCategory)
	api.POST("createCategory", middlewarre2.AuthMiddleware(), good_APIhandler.CreateCategory)
	//specification
	api.POST("getSpecification", middlewarre2.AuthMiddleware(), good_APIhandler.GetSpecification)
	api.POST("updateSpecification", middlewarre2.AuthMiddleware(), good_APIhandler.UpdateSpecification)
	api.POST("createSpecification", middlewarre2.AuthMiddleware(), good_APIhandler.CreateSpecification)
	//param
	api.POST("getParams", middlewarre2.AuthMiddleware(), good_APIhandler.GetParams)
	api.POST("updateParam", middlewarre2.AuthMiddleware(), good_APIhandler.UpdateParam)
	api.POST("createParam", middlewarre2.AuthMiddleware(), good_APIhandler.CreateParam)
	//Spu&Sku
	api.POST("createSpu", middlewarre2.AuthMiddleware(), good_APIhandler.CreateSpu)

	if err := r.Run("127.0.0.1:8080"); err != nil {
		panic(err)
	}
}

func loginMiddle() gin.HandlerFunc {

	return func(context *gin.Context) {

		byte := make([]byte, 1024)
		n, _ := context.Request.Body.Read(byte)
		fmt.Println(string(byte[0:n]))

		//gin框架请求的body数据只能读取一次，所以在middle读取后需要重新写入到body中
		buffer := new(bytes.Buffer)
		buffer.Write(byte[0:n])
		context.Request.Body = ioutil.NopCloser(buffer)
		rand.Seed(time.Now().UnixNano())
		context.Set("RequestId",common.GenerateSubId())
		context.Next()
	}
}
